//
//  NewFeatureCollectionViewController.swift
//  01.weibo
//
//  Created by 谢玉锋 on 16/7/13.
//  Copyright © 2016年 itheima.cn. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class NewFeatureCollectionViewController: UICollectionViewController {
    
    //创建布局
    let layout = UICollectionViewFlowLayout()

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //外面调用的 NewFeatureCollectionViewController() 就是调用最简单的 init () { }
    init() {
    //一定要调用父类方法
        super.init(collectionViewLayout: layout)
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        //分页和弹簧效果都是collectionView属性
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
    }

    
    override func viewDidLoad() {
        super.viewDidLoad()
        //注册cell
        self.collectionView!.registerClass(JYNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return NumberOfNewFeatureItem
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        //获取可重用cell
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! JYNewFeatureCell
        cell.backgroundColor = UIColor.randomColor()
        cell.index = indexPath.item
        return cell
    }
}
//控制器的扩展
extension NewFeatureCollectionViewController {

    override func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
//        JYPrint("结束拖拽")
    }
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
//        JYPrint("加速停止")
        //获取滑动后的下标
        let index = Int(scrollView.contentOffset.x / UIScreen.mainScreen().bounds.width)
        //根据下标获得cell
        let indexpath = NSIndexPath(forItem: index, inSection: 0)
        let cell = collectionView?.cellForItemAtIndexPath(indexpath) as! JYNewFeatureCell
        if index == NumberOfNewFeatureItem - 1 {
        cell.startAnimation()
        }
    }
}


//类中定义另一个类
class JYNewFeatureCell: UICollectionViewCell {
    
    //当前cell的对应的页数
    //默认为 0 页
    var index: Int = 0 {
        //属性检测器
        didSet {
            let imgName: String = "new_feature_" + "\(index + 1)"
            bgImageView.image = UIImage(named: imgName)
            //在设置背景图片时,就讲开始按钮隐藏
            startButton.hidden = true
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //提示: 是控制器类的 重写 init () { super.init()} ; 如果是view类的则 重写 init (frame: CGRect) {  super.init(frame: frame) }
    override init(frame: CGRect) {
        super.init(frame: frame)
        //添加到cell中, 注意cell中添加子控件 用contentView表示
        contentView.addSubview(bgImageView)
        //添加开始按钮
        contentView.addSubview(startButton)
        //添加背景图片约束
        bgImageView.snp_makeConstraints { (make) -> Void in
//            make.edges.equalTo(UIEdgeInsetsZero)  // 方法一
            make.top.leading.trailing.bottom.equalTo(contentView) // 方法二
        }
        //添加按钮约束
        startButton.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(contentView).offset(-160)
            make.centerX.equalTo(contentView)
        }
    }
    
    //开始动画
    private func startAnimation () {
        startButton.hidden = false
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        //带弹簧效果的动画
        UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 3, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
//                JYPrint("动画完成")
        }
    }
    //开始体验按钮点击事件
    @objc private func didClickStartButton () {
    
        let controller = JYMainTabBarController()
        
        (UIApplication.sharedApplication().delegate as! AppDelegate).switchController(controller)
    }

    //开始按钮
    private lazy var startButton: UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        button.setTitle("开始体验", forState: UIControlState.Normal)
        //要取到titleLablel才能设置大小
        button.titleLabel?.font = UIFont.systemFontOfSize(buttonFont)
        //添加点击事件
        button.addTarget(self, action: Selector("didClickStartButton"), forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    //懒加载子控件
    private lazy var bgImageView: UIImageView = {
        let bgImgView = UIImageView()
        bgImgView.image = UIImage(named: "new_feature_1")
        return bgImgView
    }()
}








